package com.example.demo.controller;

import com.example.demo.request.User2Request;
import com.example.demo.request.UserRequest;
import com.jayway.jsonpath.JsonPath;
import org.springframework.http.HttpEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 请求数据
 */
@RestController
@RequestMapping("/request")
public class RequestController {
    // ------------ 以下为从请求路径中获取数据 ------------
    // 入参为路径参数
    @PostMapping("/path/{id}")
    public String requestParam(@PathVariable String id) {
        return "hello " + id;
    }

    // 入参为查询参数
    @PostMapping("/query")
    public String requestQuery(@RequestParam String name, @RequestParam int age) {
        return "hello " + name + ", age is " + age;
    }

    // ------------ 以下为从请求体中获取数据 ------------
    // 入参为字符串
    @PostMapping("/string")
    public String request(HttpEntity<String> httpEntity) {
        String name = httpEntity.getBody();
        return "hello " + name;
    }

    // 入参为简单对象
    @PostMapping("/objectWithSimple")
    public String requestObjectWithSimple(@RequestBody UserRequest user) {
        System.out.println(user.toString());
        return "hello " + user.getName();
    }

    // 入参为复杂对象
    @PostMapping("/objectWithComplex")
    public String requestObjectComplex(@RequestBody User2Request user) {
        System.out.println(user.toString());
        return "hello " + user.getName();
    }

    // 入参为字符串数组
    @PostMapping("/arrayWithString")
    public String requestArrayString(@RequestBody String[] names) {
        StringBuilder sb = new StringBuilder();
        for (String name : names) {
            sb.append(name).append(" ");
        }
        System.out.println(sb.toString());
        return sb.toString();
    }

    // 入参为对象数组
    @PostMapping("/arrayWithObject")
    public String requestArrayObject(@RequestBody UserRequest[] users) {
        StringBuilder sb = new StringBuilder();
        for (UserRequest user : users) {
            sb.append(user.getName()).append(" is ").append(user.getAge()).append("岁 ");
        }
        System.out.println(sb.toString());
        return sb.toString();
    }

    // 入参为文件
    @PostMapping("/file")
    public String handleFileUpload(@RequestParam("file") MultipartFile file) {
        try {
            Path path = Paths.get(Objects.requireNonNull(file.getOriginalFilename()));
            System.out.println(path.toString());
            Files.write(path, file.getBytes());
            return "File uploaded successfully";
        } catch (IOException e) {
            e.printStackTrace();
            return "Failed to upload file";
        }
    }

    // ---------- 使用其他方式从请求体获取数据 ----------
    // 使用object
    @PostMapping("/object")
    public String requestObject(@RequestBody Object object) {
        System.out.println(object.toString());
        return "hello " + object.toString();
    }

    // 使用map
    @PostMapping("/map")
    public String requestMap(@RequestBody Map<String, Object> map) {
        System.out.println(map.toString());
        return "hello " + map.toString();
    }

    // 使用list
    @PostMapping("/list")
    public String requestList(@RequestBody List<Object> list) {
        System.out.println(list.toString());
        return "hello " + list.toString();
    }

    // 使用json
    @PostMapping("/json")
    public String requestJson(@RequestBody String body) {
        String userName = JsonPath.read(body, "$.info.name");
        int userAge = JsonPath.read(body, "$.info.age");
        return "hello " + userName + ", age is " + userAge;
    }

    // ------------ 以下为从其他部分获取数据 ------------
    // 获取请求头
    @PostMapping("/header")
    public String requestHeader(@RequestHeader("User-Agent") String userAgent) {
        return "User-Agent: " + userAgent;
    }

    // 获取cookie
    @PostMapping("/cookie")
    public String requestCookie(@CookieValue("JSESSIONID") String sessionId) {
        return "JSESSIONID: " + sessionId;
    }
}
